package com.cxb;

import java.util.Hashtable;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cxb.entity.EventBusMessage;
import com.cxb.manager.PhoneManager;
import com.cxb.manager.PhoneSignalInfo;
import com.cxb.utils.CommonUtils;
import com.cxb.utils.SharedPreferencesUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import de.greenrobot.event.EventBus;

public class RechangeIOPActivity extends BaseActivity {
	private TextView tvDecription;
	private TextView tvNoSim;
	ImageView iv_rechargeQrCode;
	private PhoneManager phoneManager;
	final String JIANGXIICCID = "XXXX89860114";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_charger_iot);
		tvDecription = (TextView) findViewById(R.id.tv_decription);
		tvNoSim = (TextView) findViewById(R.id.tv_no_sim);
		iv_rechargeQrCode = (ImageView) findViewById(R.id.iv_rechargeQrCode);
		phoneManager = new PhoneManager(this);
		initSIMInfo();
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	private void initSIMInfo() {

		if (phoneManager.isSimCardAccess()) {
			tvDecription.setVisibility(View.VISIBLE);
			tvNoSim.setVisibility(View.GONE);
			String imsi = phoneManager.getSimImsi();
			String iccid = phoneManager.getSimIccid();

			if (CommonUtils.getModel().equals("YX001D")) {
				// 亿程显示自已公司的充值提示
				Bitmap qrCodeBitmap = createImage("http://www.yxtintin.com:8280/chongzhi?tel="
						+ iccid);
				if (qrCodeBitmap != null) {
					iv_rechargeQrCode.setImageBitmap(qrCodeBitmap);
					iv_rechargeQrCode.setVisibility(View.VISIBLE);
				}
				tvDecription.setText("\n\n\n"+getString(R.string.setting_recharge_tip));
				return;
			}

			String text = getString(R.string.charge_decription_iop, iccid, imsi);
			String phoneNumber = phoneManager.getPhoneNumber();

			if (phoneNumber != null
					&& phoneManager.getSimIccid().startsWith(JIANGXIICCID)) {
				iv_rechargeQrCode.setVisibility(View.VISIBLE);
				text = getString(R.string.charge_decription_tips,
						phoneManager.getSimIccid(), phoneManager.getSimImsi());
				char[] textchars = text.toCharArray();
				for (int i = 0; i < textchars.length; i++) {
					if (textchars[i] == '\n') {
						textchars[i] = ' ';
						break;
					}
				}

				for (int i = 0; i < textchars.length; i++) {
					if (textchars[i] == '\t') {
						for (int j = i; j < textchars.length - 1; j++) {
							textchars[j] = textchars[j + 1];
						}
					}
				}

				text = new String(textchars);
				tvDecription.setText(text);
				return;
			}

			if (!TextUtils.isEmpty(phoneNumber)) {
				text += "\n\t" + getString(R.string.phone_number) + phoneNumber;
			}
			tvDecription.setText(text);
		} else {
			tvDecription.setVisibility(View.GONE);
			tvNoSim.setVisibility(View.VISIBLE);
			tvNoSim.setText(R.string.sim_error);
		}

	}

	public void onEventMainThread(EventBusMessage event) {

		initSIMInfo();
	}

	private static int QR_SIDE = 550;

	private Bitmap createImage(String text) {
		try {
			if (text == null || "".equals(text) || text.length() < 1) {
				return null;
			}
			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			BitMatrix bitMatrix = new QRCodeWriter().encode(text,
					BarcodeFormat.QR_CODE, QR_SIDE, QR_SIDE, hints);
			int[] pixels = new int[QR_SIDE * QR_SIDE];
			for (int y = 0; y < QR_SIDE; y++) {
				for (int x = 0; x < QR_SIDE; x++) {
					if (bitMatrix.get(x, y)) {
						pixels[y * QR_SIDE + x] = 0xff000000;
					} else {
						pixels[y * QR_SIDE + x] = 0xffffffff;
					}

				}
			}

			Bitmap bitmap = Bitmap.createBitmap(QR_SIDE, QR_SIDE,
					Bitmap.Config.ARGB_8888);

			bitmap.setPixels(pixels, 0, QR_SIDE, 0, 0, QR_SIDE, QR_SIDE);
			return bitmap;

		} catch (WriterException e) {
			e.printStackTrace();
		}
		return null;
	}
}
